package dao.impl;

import dao.ITeacherDao;
import domain.Teacher;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
 * 老师数据访问层的具体实现方式
 * 实现了数据访问层的接口
 */
public class TeacherDao implements ITeacherDao {
    @Override
    public boolean selectTeacher(String workno, String pwd) {
        //初始化连接
        Connection connection  = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "select * from teacher where workno = '" + workno + "' and pwd='" + pwd + "'";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);
        //获取结果集
        ResultSet rs = JDBCUtil.getRs(pstmt);
        //使用rs.next（）迭代数据，如果有下一条，则返回真，否则返回假
        try {
            //如果结果集当中有数据则说明登录数据存在，否则不存在
            if(rs.next()){
                return  true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs,pstmt,connection);
        }
        return false;
    }

    //查询管理员
    public List selectManagers() {
        //需要到数据库中获取数据
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "select * from teacher";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);
        //获取结果集
        ResultSet rs = JDBCUtil.getRs(pstmt);

        //初始化一个list容器用于放置Teacher的数据
        List<Teacher> teacherList = new ArrayList<Teacher>();
        //使用rs.next（）迭代数据，如果有下一条，则返回真，否则返回假
        try {
            while (rs.next()) {
                //将所有的数据库数据转化为java中的对象，完成数据结构的转换
                Teacher teacher = new Teacher();
                teacher.setName(rs.getString("name"));
                teacher.setWorkno(rs.getString("workno"));
                teacher.setCollege(rs.getString("college"));
                teacher.setSex(rs.getString("sex"));
                teacher.setPwd(rs.getString("pwd"));
                //将数据赋给list容器
                teacherList.add(teacher);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pstmt, connection);
        }
        return teacherList;
    }

    //添加管理员
    public int insertManager(Teacher teacher) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "insert into teacher (workno,sex,college,name,pwd) values(?,?,?,?,?) ";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集
        try {

            pstmt.setString(1, teacher.getWorkno());
            pstmt.setString(2, teacher.getSex());
            pstmt.setString(3, teacher.getCollege());
            pstmt.setString(4, teacher.getName());
            pstmt.setString(5, teacher.getPwd());

        } catch (SQLException e) {
            e.printStackTrace();
        }
        int result = JDBCUtil.modify(pstmt);
        return result;
    }
    //删除管理员
    public String deleteManager(String num) {//初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "delete from teacher where workno='"+num+"'";

        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        int result = JDBCUtil.modify(pstmt);

        return sql;
    }


}